package com.adjy.pathbuilders.screens;

import static com.adjy.pathbuilders.commons.Preconditions.checkNotNull;

import java.util.List;

import android.view.MotionEvent;

import com.adjy.pathbuilders.objects.button.Button;
import com.adjy.pathbuilders.objects.button.ImageButton;
import com.adjy.pathbuilders.objects.button.listeners.ButtonListener;
import com.adjy.pathbuilders.rendering.GameCanvas;
import com.adjy.pathbuilders.rendering.loading.AnimationFactory;
import com.adjy.pathbuilders.rendering.loading.WeakReferenceBitmap;

public class ScreenPage implements GameScreen {
	
	private List<Button> buttons;
	
	private final WeakReferenceBitmap pageBackground;
	private AnimationFactory factory;
	private final Button exitButton;
	
	private ScreenPage previous = null;
	private ScreenPage next = null;
	
	private ScreenPage nextPage = null;
	
	private ImageButton previousButton = null;
	private ImageButton nextButton = null;
	
	private static final int previousButtonX = 20;
	private static final int previousButtonY = 380;
		
	private static final int nextButtonX = 700;
	private static final int nextButtonY = 380;
	
	public ScreenPage(WeakReferenceBitmap background, AnimationFactory anFactory, Button exitButton, List<Button> buttons) {
		checkNotNull(background, "background must not be null.");
		checkNotNull(anFactory, "factory must not be null.");
		checkNotNull(exitButton, "exitButton must not be null.");
		
		this.factory = anFactory;
		this.buttons = buttons;
		
		this.pageBackground = background;
		nextPage = this;
		this.exitButton = exitButton;
	}

	public ScreenPage getPrevious() {
		return previous;
	}

	public void setPrevious(final ScreenPage previous) {
		checkNotNull(previous, "previous must not be null.");
		this.previous = previous;
		previousButton = new ImageButton(previousButtonX, previousButtonY,
				new GoToPreviousButtonListener(),
				new WeakReferenceBitmap(factory, "previous_selected"),
				new WeakReferenceBitmap(factory, "previous_unselected"));
	}
	
	public ScreenPage getNext() {
		return next;
	}

	public void setNext(final ScreenPage next) {
		checkNotNull(next, "next must not be null.");
		this.next = next;
		
		nextButton = new ImageButton(nextButtonX, nextButtonY,
					new GoToNextButtonListener(),
					new WeakReferenceBitmap(factory, "next_selected"),
					new WeakReferenceBitmap(factory, "next_unselected"));
	}

	@Override
	public void load() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void release() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void input(MotionEvent event) {
		exitButton.input(event);
		
		// FIXME very ugly
		if (nextButton != null) {
			nextButton.input(event);			
		}
		
		// FIXME very ugly
		if (previousButton != null) {
			previousButton.input(event);			
		}

		for (Button button : buttons) {
			button.input(event);
		}
	}

	@Override
	public void render(GameCanvas mcanvas, float timeElapsed) {
		mcanvas.drawBackground(pageBackground.getBitmap());
		exitButton.render(mcanvas, timeElapsed);
		
		// FIXME very ugly
		if (nextButton != null) {
			nextButton.render(mcanvas, timeElapsed);			
		}
		// FIXME very ugly
		if (previousButton != null) {
			previousButton.render(mcanvas, timeElapsed);			
		}
		
		for (Button button : buttons) {
			button.render(mcanvas, timeElapsed);
		}
	}

	@Override
	public void step(float timePassed) {
		// TODO Auto-generated method stub
		
	}

	public ScreenPage getNextPage() {
		return nextPage;
	}
	
	public void reset() {
		nextPage = this;
	}
	
	private class GoToNextButtonListener implements ButtonListener {
		@Override
		public void onButtonPressed() {
			nextPage = next;
		}
	}
	
	private class GoToPreviousButtonListener implements ButtonListener {
		@Override
		public void onButtonPressed() {
			nextPage = previous;
		}
	}
}
